package cn.net.shuxing.yifu;

import cn.net.shuxing.yifu.common.bean.*;
import cn.net.shuxing.yifu.common.constant.DealState;
import cn.net.shuxing.yifu.common.service.*;
import cn.net.shuxing.yifu.common.util.JedisUtil;
import cn.net.shuxing.yifu.common.util.RedisKeyCreator;
import cn.net.shuxing.yifu.common.util.SpringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.bind.annotation.CrossOrigin;
import redis.clients.jedis.Jedis;
import tk.mybatis.spring.annotation.MapperScan;

import java.math.BigDecimal;
import java.util.List;

@SpringBootApplication
@EnableTransactionManagement
@MapperScan(basePackages = {"cn.net.shuxing.yifu.common.mapper", "cn.net.shuxing.yifu.mysiteforme.admin.dao"})
@EnableScheduling
public class YifuApplication extends SpringBootServletInitializer {

    @Autowired
    private RedisDataService redisDataService;

    @Autowired
    private JedisUtil jedisUtil;


    public static void main(String[] args) throws Exception {

        ApplicationContext app = SpringApplication.run(YifuApplication.class, args);
        SpringUtil.setApp(app);
        initRedis();
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(YifuApplication.class);
    }


    public static void initRedis() throws Exception {
        ApplicationContext app = SpringUtil.getApp();
        RedisDataService redisDataService = app.getBean(RedisDataService.class);
        List<RedisData> all = redisDataService.getAllWithoutComplete();
        JedisUtil jedisUtil = app.getBean(JedisUtil.class);
        //Jedis //jedis = jedisUtil.getJedis();
        for (RedisData redisData : all) {
            jedisUtil.setWithoutUpdate(redisData.getRedisKey(),redisData.getRedisValue());
        }
        //jedisUtil.close();

        OpenPriceHistoryService openPriceHistoryService = app.getBean(OpenPriceHistoryService.class);
        CoinTypeService coinTypeService = app.getBean(CoinTypeService.class);
        List<CoinType> coinTypeList = coinTypeService.getAllWithoutComplete();
        for (CoinType coinType : coinTypeList) {
            Integer coinTypeId = coinType.getId();
            BigDecimal openPrice = openPriceHistoryService.getByCoinTypeId(coinTypeId);
            jedisUtil.set(RedisKeyCreator.openPrice(coinTypeId),openPrice.toString());
        }


        HangSaleService hangSaleService = app.getBean(HangSaleService.class);
        List<HangSale> hangSaleList = hangSaleService.getByState(DealState.DEALING);
        for (HangSale hangSale : hangSaleList) {
            hangSaleService.setInRedis(hangSale);
        }

        HangBuyService hangBuyService = app.getBean(HangBuyService.class);
        List<HangBuy> hangBuyList = hangBuyService.getByState(DealState.DEALING);
        for (HangBuy hangBuy : hangBuyList) {
            hangBuyService.setInRedis(hangBuy);
        }
    }
}

/*
3. 修改后台广告(banner)前后端
 */